package com.wujige.advancedQuery;

import com.wujige.utils.ClinetUtils;
import org.apache.lucene.search.PointInSetQuery;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.stream.Stream;

/**
 * 测试高级查询
 * @author: lipan
 * @date: 2021/4/27 20:25
 */

public class AdvanceQueryTest {


    RestHighLevelClient client = null;


    @Before
    public void OpenConection(){
        client= ClinetUtils.getClient();
    }

    @After
    public void closeConnection(){
        ClinetUtils.close();
    }


    /**
     * 通用查询方法封装
     */
    private void commonQuery(String index,QueryBuilder queryBuilder,SearchSourceBuilder searchSourceBuilder) {

        SearchRequest searchRequest=new SearchRequest();

        searchRequest.indices(index);

        searchSourceBuilder.query(queryBuilder);

        searchRequest.source(searchSourceBuilder);

        SearchResponse search = null;
        try {
            search = client.search(searchRequest, RequestOptions.DEFAULT);
            SearchHits hits = search.getHits();

            System.out.println("耗时:"+search.getTook());
            System.out.println("查询总条数:"+hits.getTotalHits() );

            Stream.of(hits.getHits()).map(h->h.getSourceAsString()).forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }



    }



    /**
     * 查询当前user索引全部数据
     * @throws Exception
     */
    @Test
    public void testMatchAll() throws Exception{

        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();

        SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.searchSource();

        commonQuery("user",matchAllQueryBuilder,searchSourceBuilder);
    }



    @Test
    public void testTermQuery() throws Exception{

        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("age", 20);


        SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.searchSource();


        commonQuery("user",termQueryBuilder,searchSourceBuilder);

    }


    /**
     * 分页测试
     * @throws Exception
     */
    @Test
    public void testPagination() throws Exception{


        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();

        SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.searchSource();

        searchSourceBuilder.from(0);
        searchSourceBuilder.size(3);
        commonQuery("user",matchAllQueryBuilder,searchSourceBuilder);



     }


    /**
     * 排序测试
     * @throws Exception
     */
     @Test
     public void testOrder() throws Exception{

         SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
         sourceBuilder.sort("age", SortOrder.ASC);
         commonQuery("user",QueryBuilders.matchAllQuery(),sourceBuilder);
     }


    /**
     * 过滤字段
     * @throws Exception
     */
     @Test
     public void testFilter() throws Exception{
         SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
         sourceBuilder.fetchSource(new String[]{"age","name"},new String[]{});
         commonQuery("user",QueryBuilders.matchAllQuery(),sourceBuilder);

     }

    /**
     * 组合查询
     * @throws Exception
     */
     @Test
     public void test() throws Exception{

         BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();

         boolQuery.must(QueryBuilders.matchQuery("age",30));
         boolQuery.mustNot(QueryBuilders.matchQuery("name","zhangsan"));
         boolQuery.should(QueryBuilders.matchQuery("sex","女"));

         commonQuery("user",boolQuery,SearchSourceBuilder.searchSource());

     }

    /**
     * 范围查询
     * @throws Exception
     */
     @Test
     public void testComQuery() throws Exception{

         RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age");
         //年龄大于等于20
         rangeQueryBuilder.gte(20);

         //年龄小于等于60
         rangeQueryBuilder.lte(60);

         commonQuery("user",rangeQueryBuilder,SearchSourceBuilder.searchSource());

     }

    /**
     * 模糊查询
     * @throws Exception
     */
     @Test
     public void testFuzzQuery() throws Exception{

         //fuzziness(Fuzziness.TWO) 代表 差了几个不同
         FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("name", "on").fuzziness(Fuzziness.TWO);

         commonQuery("user",fuzzyQueryBuilder,SearchSourceBuilder.searchSource());

     }
}
